<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

/**
 * Class OutDetail
 * @package app\home\controller
 * if you see here
 * run fast
 */
class OutDetail extends BaseController
{
    protected $title='出库明细';
    protected $table='stock_mx_view';
    private $sessionKey='pageOutDetail';

    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @autho:xiangzi
     * @auth:true
     * @Date:2025/4/14
     */
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['mdId','>',0];
            $where[]=['type','in',[1,6]];
//            foreach (['bar_code','mdName','specs','brand'] as $i){
//                if (isset($param[$i])&&$param[$i]){
//                    $where[]=[$i,'like',"%".$param[$i].'%'];
//                }
//            }
            if (isset($param['keywords'])&&$param['keywords']){
                $where[]=['bar_code|mdName|brand|specs|buyCode|taxCode|saleContractOrder','like',"%".$param['keywords'].'%'];
            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['brand','like',"%".$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['mdName','like',"%".$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['specs','like',"%".$param['specs'].'%'];
            }
            if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['buyCode','like',"%".$param['buyCode'].'%'];
            }
            if (isset($param['taxCode'])&&$param['taxCode']){
                $where[]=['taxCode','like',"%".$param['taxCode'].'%'];
            }
            if (isset($param['cgrkd'])&&$param['cgrkd']){
                $cgMxId=Db::name($this->table)
                    ->where('type',3)
                    ->where('status',2)
                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
                    ->column('saleContractDetailId');
                $xsmxId=Db::name('sale_contract_detail')
                    ->where('status',4)
                    ->where('type',2)
                    ->where('id','in',$cgMxId)
                    ->column('saleContractDetailId');
                $where[]=['saleContractDetailId','in',$xsmxId];
            }
//            halt($where);
            if (isset($param['cght'])&&$param['cght']){
//                $where[]=['saleContractOrder','like',"%".$param['cght'].'%'];
                $cgHtMxId=Db::name('sale_contract_detail')
                    ->alias('scd')
                    ->leftJoin('sale_contract sc','sc.id=scd.orderId')
                    ->where('sc.status',4)
                    ->where('sc.type',2)
                    ->where('sc.orderNum','like','%'.$param['cght'].'%')
                    ->column('scd.saleContractDetailId');
                $where[]=['saleContractDetailId','in',$cgHtMxId];
            }

            if (isset($param['pName'])&&$param['pName']){
                $where[]=['pName','like',"%".$param['pName'].'%'];
            }
            if (isset($param['stockNum'])&&$param['stockNum']){
                $where[]=['stokcOrderNum','like',"%".$param['stockNum'].'%'];
            }
            if (isset($param['type'])&&$param['type']){
                $types=$this->getAllSubIds($param['type']);
                $types[]=$param['type'];
                $where[]=['mdType','in',$types];
            }
            if (isset($param['warehouse_id'])&&$param['warehouse_id']){
                $where[]=["wId","=",$param['warehouse_id']];
            }
            if (isset($param['times'])&&$param['times']){
                $times=explode('~',$param['times']);
                $where[]=['checkTime','between',$times];
            }
            if (isset($param['hsContract'])&&$param['hsContract']){
                $where[]=['hsContract','like',"%".$param['hsContract'].'%'];
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->where($where)
                ->field("*")
                ->order('id DESC')->paginate($rows)->each(function ($item,$index){//逻辑更改严重
//                    $whear1[]=['id','=',$item['saleContractDetailId']];
//                    $scdInfo=Db::name('sale_contract_detail')
//                        ->where($whear1)->find();
//                    $dj=$scdInfo['bjPrice'];
                    $dj=$item['sodPrice'];
                    $je=$item['num']*$dj;
                    $item['dj']=$dj;
                    $item['je']=round($je,2);
                    $cghtOrder=[];
                    $cgrkOrder=[];
//                    if ($item['saleContractDetailId']){
//                        $cghtOrder=Db::name('sale_contract_detail')
//                            ->alias('scd')
//                            ->leftJoin('sale_contract sc','sc.id=scd.orderId')
//                            ->where('scd.saleContractDetailId',$item['saleContractDetailId'])
//                            ->where('scd.status',4)
//                            ->where('scd.type',2)
//                            ->column('sc.orderNum');

//                        $cgrkOrder=Db::name('stock_order_detail')
//                            ->alias('sod')
//                            ->leftJoin('stock_order so','so.id=sod.orderId')
//                            ->where('sod.type',3)
//                            ->where('sod.status',2)
//                            ->where('sod.saleContractOrder','in',$cghtOrder)
//                            ->column('so.orderNum');

//                    }
//                    $item['cghtOrder']=implode(',',array_unique($cghtOrder));
//                    $item['cgRkOrder']=implode(',',array_unique($cgrkOrder));
                    $item['cghtOrder']=$item['buyContractOrder'];
                            $cgrkOrder=Db::name('stock_order_detail')
                            ->alias('sod')
                            ->leftJoin('stock_order so','so.id=sod.orderId')
                            ->where('sod.type',3)
                            ->where('sod.status',2)
                            ->where('sod.saleContractOrder','in',$item['cghtOrder'])
                            ->column('so.orderNum');
                    $item['cgRkOrder']=implode(',',array_unique($cgrkOrder));;
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
            View::assign('warehouseList',$warehouse);
            return view();
        }

    }
}